function db($dbfile)
{
	//连接数据库
	if(!$dbfile)	$dbfile = "data.db";
	this.conn = new air.SQLConnection(); 
	var dbFile = air.File.applicationStorageDirectory.resolvePath(air.File.applicationDirectory.nativePath + "/" + $dbfile); 
	
	try 
	{
		this.conn.open(dbFile);
		air.trace("the database was connect successfully"); 
	} 
	catch (error) 
	{
		air.trace("Error message:", error.message); 
		air.trace("Details:", error.details); 
	}
	
	//关闭数据库连接
	this.close = function()
	{
		this.conn.close();	
	}
	
	//执行一条SQL语句，不返回任何结果
	this.execute = function($sql, $param)
	{
		var sql_statement = new air.SQLStatement();
		sql_statement.sqlConnection = this.conn;
		sql_statement.text = $sql;
		
		if($param)
		{
			$.each($param, function(k, v){
				if(!(/^[0-9]+$/.test(k)))
				{
					k = ":"+k;
				}
				sql_statement.parameters[k] = v
			});
		}
		air.trace("SQL:"+sql_statement.text);

		
		try 
		{ 
			sql_statement.execute(); 
			return sql_statement;
		}
		catch (e) 
		{
			$.each(e, function(k,v){
				alert(k+"\n\n"+v);
			});
			var $msg = "SQL Error.\n";
			$msg += "Message: "+ e.toString() + "\n";
			$msg += "SQL: " + $sql +"\n";	
			air.trace($msg);
			alert($msg);
			return false;
			// Information about the error is available in the 
			// error variable, which is an instance of  
			// the SQLError class. 
		}
	}
	
	//执行一条SQL语句，返回所有数据
	this.fetch_all = function($sql, $param)
	{
		var result_data = this.execute($sql, $param);
		return result_data.getResult().data;
	}
	
	//取得一行数据
	this.fetch_row = function($sql, $param)
	{
		var $result = this.fetch_all($sql, $param);
		return $result && $result[0] ? $result[0] : false;
	}
	
	//取得最后一次执行产生的ID
	this.last_id = function(sql_statement)
	{
		return sql_statement.getResult().lastInsertRowID;
	}
	

	//插入一条数据
	this.insert = function($table, $data, $ignore)
	{
		$ignore = $ignore ? "IGNORE" : "";
		var $sql = "INSERT "+ $ignore +" INTO " + $table + " ";

		//整理数据的KEY与Value
		var $key="", $value="";
		$.each($data, function(k, v){
			$key += k + ",";
			$value += ":" + k + ",";
		});
		$key = $key.substr(0, $key.length-1);
		$value = $value.substr(0, $value.length-1);
		
		$sql += "("+$key+") VALUES ("+$value+")";
		
		return this.execute($sql, $data);
	}
	
	//更新一条数据
	this.update = function($table, $data)
	{
		if(!$type || !$table || !$data)	return false;		
		
		var $sql = $type + " " +$table+ " SET ";
		$.each($data, function(k, v){
			$sql += k + " = :" +k+ " ,";
		});
		$sql = $sql.substr(0, $sql.length-1);
		return this.execute($sql, $data);
	}
	
	//开始一个事务
	this.begin = function()
	{
		this.conn.begin();
		air.trace("DB:begin");
	}
	
	//提交一个事务
	this.commit = function()
	{
		this.conn.commit();
		air.trace("DB:commit");
	}
	
	//回滚一个事务
	this.rollback = function()
	{
		this.conn.rollback();
		air.trace("DB:rollback");		
	}
	
	
}